home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / osrc.arc / ENET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-18  |  3.1 KB  |  150 lines

  1. /* Stuff generic to all Ethernet controllers */
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "iface.h"
  5. #include "timer.h"
  6. #include "arp.h"
  7. #include "enet.h"
  8.  
  9. char *res_arp();
  10. void arp_input();
  11.  
  12. char Ether_bdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  13.  
  14. /* Convert Ethernet header in host form to network mbuf */
  15. struct mbuf *
  16. htonether(ether,data)
  17. struct ether *ether;
  18. struct mbuf *data;
  19. {
  20.     struct mbuf *bp;
  21.     register char *cp;
  22.  
  23.     if((bp = pushdown(data,ETHERLEN)) == NULLBUF)
  24.         return NULLBUF;
  25.  
  26.     cp = bp->data;
  27.  
  28.     memcpy(cp,ether->dest,EADDR_LEN);
  29.     cp += EADDR_LEN;
  30.     memcpy(cp,ether->source,EADDR_LEN);
  31.     cp += EADDR_LEN;
  32.     put16(cp,ether->type);
  33.  
  34.     return bp;
  35. }
  36. /* Extract Ethernet header */
  37. int
  38. ntohether(ether,bpp)
  39. struct ether *ether;
  40. struct mbuf **bpp;
  41. {
  42.     pullup(bpp,ether->dest,EADDR_LEN);
  43.     pullup(bpp,ether->source,EADDR_LEN);
  44.     ether->type = pull16(bpp);
  45.     return ETHERLEN;
  46. }
  47.  
  48. /* Format an Ethernet address into a printable ascii string */
  49. int
  50. pether(out,addr)
  51. char *out,*addr;
  52. {
  53.     return sprintf(out,"%02x:%02x:%02x:%02x:%02x:%02x",
  54.         uchar(addr[0]),uchar(addr[1]),
  55.         uchar(addr[2]),uchar(addr[3]),
  56.         uchar(addr[4]),uchar(addr[5]));
  57. }
  58.  
  59. /* Convert an Ethernet address from Hex/ASCII to binary */
  60. int
  61. gether(out,cp)
  62. register char *out;
  63. register char *cp;
  64. {
  65.     register int i;
  66.  
  67.     for(i=6; i!=0; i--){
  68.         *out++ = htoi(cp);
  69.         if((cp = strchr(cp,':')) == NULLCHAR)    /* Find delimiter */
  70.             break;
  71.         cp++;            /* and skip over it */
  72.     }
  73.     return i;
  74. }
  75. /* Version of gether for ARP, matches passed args */
  76. gaether(out,in,cnt)
  77. char *out;
  78. char *in[];
  79. int cnt;
  80. {
  81.     return gether(out,in[0]);
  82. }
  83. /* Send an IP datagram on Ethernet */
  84. int
  85. enet_send(bp,iface,gateway,prec,del,tput,rel)
  86. struct mbuf *bp;    /* Buffer to send */
  87. struct iface *iface;    /* Pointer to interface control block */
  88. int32 gateway;        /* IP address of next hop */
  89. int prec;
  90. int del;
  91. int tput;
  92. int rel;
  93. {
  94.     char *egate;
  95.  
  96.     egate = res_arp(iface,ARP_ETHER,gateway,bp);
  97.     if(egate != NULLCHAR)
  98.         return (*iface->output)(iface,egate,iface->hwaddr,IP_TYPE,bp);
  99.     return 0;
  100. }
  101. /* Send a packet with Ethernet header */
  102. int
  103. enet_output(iface,dest,source,type,data)
  104. struct iface *iface;    /* Pointer to interface control block */
  105. char dest[];        /* Destination Ethernet address */
  106. char source[];        /* Source Ethernet address */
  107. int16 type;        /* Type field */
  108. struct mbuf *data;    /* Data field */
  109. {
  110.     struct ether ep;
  111.     struct mbuf *bp;
  112.  
  113.     memcpy(ep.dest,dest,EADDR_LEN);
  114.     memcpy(ep.source,source,EADDR_LEN);
  115.     ep.type = type;
  116.     if((bp = htonether(&ep,data)) == NULLBUF){
  117.         free_p(data);
  118.         return -1;
  119.     }
  120.     return (*iface->raw)(iface,bp);
  121. }
  122. /* Process incoming Ethernet packets. Shared by all ethernet drivers. */
  123. void
  124. eproc(iface,bp)
  125. struct iface *iface;
  126. struct mbuf *bp;
  127. {
  128.     struct ether hdr;
  129.     char multicast;
  130.  
  131.     /* Remove Ethernet header and kick packet upstairs */
  132.     ntohether(&hdr,&bp);
  133.     if(hdr.dest[0] & 1)
  134.         multicast = 1;
  135.     else
  136.         multicast = 0;
  137.     switch(hdr.type){
  138.     case ARP_TYPE:
  139.         arp_input(iface,bp);
  140.         break;
  141.     case IP_TYPE:
  142.         ip_route(bp,multicast);
  143.         break;
  144.     default:
  145.         free_p(bp);
  146.         break;
  147.     }
  148. }
  149.  
  150.